﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class News : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.bindItemsList();
        }
        
    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


    ///for paging
    PagedDataSource _PageDataSource = new PagedDataSource();
    int pageCount;
    private int CurrentPage
    {
        get
        {
            object objPage = ViewState["_CurrentPage"];
            int _CurrentPage = 0;
            if (objPage == null)
            {
                _CurrentPage = 0;
            }
            else
            {
                _CurrentPage = (int)objPage;
            }
            return _CurrentPage;
        }
        set { ViewState["_CurrentPage"] = value; }
    }


    private void bindItemsList()
    {
        //        DataTable dataTable =this.GetDataTable();
        //        _PageDataSource.DataSource =dataTable.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = 10;
        _PageDataSource.CurrentPageIndex = CurrentPage;

        //here
        DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);

        _PageDataSource.DataSource = dv;
        pageCount = _PageDataSource.PageCount;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        this.dListItems.DataSource = _PageDataSource;
        //this.dListItems.DataSource = SqlDataSource1;
        this.dListItems.DataBind();
        this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount;
        this.LinkButtonPre.Enabled = !_PageDataSource.IsFirstPage;
        this.LinkButtonNext.Enabled = !_PageDataSource.IsLastPage;
        this.LinkButtonFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.LinkButtonLast.Enabled = !_PageDataSource.IsLastPage;
        this.doPaging();
    }

    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        int fistIndex = 0;//should be correct text
        int lastIndex = Math.Min(pageCount, 10);//should be correct text

        for (int i = fistIndex; i < lastIndex; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;//should be correct text
            dt.Rows.Add(dr);
        }
        this.dlPaging.DataSource = dt;
        this.dlPaging.DataBind();
    }
    ///click on linkboutton fire this method
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {

        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            this.bindItemsList();
        }
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Style.Add("fone-size", "14px");
            lnkbtnPage.Font.Bold = true;
        }
    }
    protected void LinkButtonNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        this.bindItemsList();
    }
    protected void LinkButtonPre_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        this.bindItemsList();
    }
    protected void LinkButtonFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 0;
        this.bindItemsList();
    }
    protected void LinkButtonLast_Click(object sender, EventArgs e)
    {
        CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
        this.bindItemsList();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }

}